/**
 * @file 动态添加权限管理
 * @author lishaohai
 */

import router, {constantRoutes} from '@/router';
import store from '@/store';
import {Loading} from 'element-ui';
import {colorFill} from '@/styles/variables.scss';

let loading;
function startLoading() {
    // 使用Element loading-start 方法
    loading = Loading.service({
        lock: true,
        text: '加载中...',
        background: colorFill
    });
}
function endLoading() {
    // 使用Element loading-close 方法
    loading.close();
}
// startLoading();
// store
//     .dispatch('app/getInfo')
//     .then(accessRoute => {
//         router.addRoutes([...constantRoutes, ...accessRoute]);
//         endLoading();
//     })
//     .catch(error => {
//         router.addRoutes(constantRoutes);
//         router.push({path: '/404'});
//         endLoading();
//         Message({type: 'error', message: error || 'Has Error!'}); // eslint-disable-line
//         console.log('error', error);
//     });

export default async function t() {
    startLoading();
    try {
        const accessRoute = await store.dispatch('app/getInfo');
        router.addRoutes([...constantRoutes, ...accessRoute]);
        endLoading();
    } catch (error) {
        router.addRoutes(constantRoutes);
        endLoading();
        store.dispatch('user/handleLogout');
        // router.push({path: LOGIN});
        // console.log('permission error', error);
        // Message({showClose: true, type: 'error', message: error || 'Has Error!'}); // eslint-disable-line
    }
}
